package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.ResultConstant;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @Reference(timeout = 3000)
    private BrandService brandService;

    @RequestMapping("list")
    public String list(){
        return "admin/brand";
    }


    //查询
    @RequestMapping("brandList")
    @ResponseBody
    public Result brandList(){
        try{
            List<TbBrand> brandList = brandService.brandList();
            return Result.response(ResultEnum.SUCCESS).setData(brandList);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }

    }

    //分页查询
    @RequestMapping("brandPage")
    @ResponseBody
    public Result brandPage(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "3") Integer pageSize) {
        try {
            Page<TbBrand> page = brandService.brandPage(pageNumber, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(page);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //回显
    @RequestMapping("getBrandById")
    @ResponseBody
    public Result getBrandById(Long id){
        try{
            TbBrand brand = brandService.getBrandById(id);
            return Result.response(ResultEnum.SUCCESS).setData(brand);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }

    }

    //增加
    @RequestMapping("addModal")
    @ResponseBody
    public Result addModal(TbBrand tbBrand) {
        try {
            Boolean b = brandService.addModal(tbBrand);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /*修改*/
    @RequestMapping("updateModal")
    @ResponseBody
    public Result updateModal(TbBrand tbBrand){
        try {
            Boolean b = brandService.updateModal(tbBrand);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /*删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Result deleteById(Long id){
        try {
            Boolean b = brandService.deleteById(id);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /*批量删除*/
    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(@RequestParam("id[]") Integer[] id){
        try {
            Boolean b = brandService.deleteInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

}
